Function Initialize{
	[void][System.Reflection.Assembly]::LoadWithPartialName("MySql.Data")

	$connection = New-Object MySql.Data.MySqlClient.MySqlConnection
	$connectionString = "server=localhost;uid=root;pwd=;database=ancestra_static;"
	$connection.ConnectionString = $connectionString
	$connection.Open()
	
	$connection
}

Function Request($sql, $connection){
	$command = New-Object MySql.Data.MySqlClient.MySqlCommand($sql, $connection)
	$dataAdapter = New-Object MySql.Data.MySqlClient.MySqlDataAdapter($command)
	$dataSet = New-Object System.Data.DataSet
	$recordCount = $dataAdapter.Fill($dataSet, "data")
	
	$dataset
}

Function Close($connection){
	$connection.Close()
}


$webclient = new-object System.Net.WebClient
$webclient.UseDefaultCredentials = $true
$webclient.Proxy.Credentials = $webclient.Credentials
  
$connection = Initialize

$sql = "select * from monsters"

$dataset = Request $sql $connection

$nbRows = $dataSet.Tables["data"].Rows.Count

$cpt = 0

While($cpt -lt $nbRows){
	
	$name = $dataSet.Tables["data"].Rows[$cpt][1]
	$grades = $dataSet.Tables["data"].Rows[$cpt][4]
	
	$nbLvl = [regex]::matches($grades,"\|").count
	$nbLvl ++
	
	If($nbLvl -eq 5){
		$pos = $grades.IndexOf("|")
		$grade1 = $grades.substring(0, $pos)
	
		$grades = $grades.substring($pos + 1)
		
		$pos = $grades.IndexOf("|")
		$grade2 = $grades.substring(0, $pos)
		
		$grades = $grades.substring($pos + 1)
		
		$pos = $grades.IndexOf("|")
		$grade3 = $grades.substring(0, $pos)
		
		$grades = $grades.substring($pos + 1)
		
		$pos = $grades.IndexOf("|")
		$grade4 = $grades.substring(0, $pos)
		
		$grade5 = $grades.substring($pos + 1)
		
		$pos = $grade1.IndexOf("@")
		$lvl1 = $grade1.substring(0, $pos)
		
		$pos = $grade2.IndexOf("@")
		$lvl2 = $grade2.substring(0, $pos)
		
		$pos = $grade3.IndexOf("@")
		$lvl3 = $grade3.substring(0, $pos)
		
		$pos = $grade4.IndexOf("@")
		$lvl4 = $grade4.substring(0, $pos)
		
		$pos = $grade5.IndexOf("@")
		$lvl5 = $grade5.substring(0, $pos)
		
		
		
		$url = "http://www.dofus.com/fr/mmorpg-jeux/monstres/search?se=$name"
		$source = $webclient.DownloadString($url)
		
		$pos = $source.IndexOf('<div class="result_nb">')
		
		If($pos -ne -1){
			$pos = $pos + 46
			
			$source = $source.substring($pos)
			
			$pos = $source.IndexOf('<')
			
			$nbResults = $source.substring(0, $pos)

			Write-host $name $nbLvl $lvl1 $lvl2 $lvl3 $lvl4 $lvl5 $nbResults
		}
		Else{
			Write-host "Name $name not found" -foregroundcolor red
			"$name" | Out-File C:\Users\luro\Desktop\Log.txt -append
		}
	}
	Else{
		If($nbLvl -eq 6){
			$pos = $grades.IndexOf("|")
			$grade1 = $grades.substring(0, $pos)
		
			$grades = $grades.substring($pos + 1)
			
			$pos = $grades.IndexOf("|")
			$grade2 = $grades.substring(0, $pos)
			
			$grades = $grades.substring($pos + 1)
			
			$pos = $grades.IndexOf("|")
			$grade3 = $grades.substring(0, $pos)
			
			$grades = $grades.substring($pos + 1)
			
			$pos = $grades.IndexOf("|")
			$grade4 = $grades.substring(0, $pos)
			
			$grades = $grades.substring($pos + 1)
			
			$pos = $grades.IndexOf("|")
			$grade5 = $grades.substring(0, $pos)
			
			$grade6 = $grades.substring($pos + 1)
			
			Write-host $name $nbLvl $grade1 $grade2 $grade3 $grade5 $grade6
			
			$url = "http://www.dofus.com/fr/mmorpg-jeux/monstres/search?se=$name"
			$source = $webclient.DownloadString($url)
			
			$pos = $source.IndexOf('<div class="result_nb">')
			
			If($pos -ne -1){
				$pos = $pos + 46
				
				$source = $source.substring($pos)
				
				$pos = $source.IndexOf('<')
				
				$nbResults = $source.substring(0, $pos)

				Write-host $name $nbLvl $lvl1 $lvl2 $lvl3 $lvl4 $lvl5 $nbResults
			}
			Else{
				Write-host "Name $name not found" -foregroundcolor red
				"$name" | Out-File C:\Users\luro\Desktop\Log.txt -append
			}
				
		}
		Else{
			Write-host $name $nbLvl -foregroundcolor red
		}
	}
	
	$posPart1 = $grades.LastIndexOf("|")
	$lvl6 = $grades.substring($posPart1 + 1)
	
	
	
	#$part2 = $sort.substring($posPart1 + 1)
	#2@1;5;5;-9;-9;5;3|3@2;6;6;-8;-8;6;4|4@3;7;7;-7;-7;7;5|5@4;8;8;-6;-6;8;6|6@5;9;9;-5;-5;9;7|1@
	$cpt ++
}

write-host $nbRows

Close $connection

#$webclient = new-object System.Net.WebClient

#$source = $webclient.DownloadString($url)